package com.blueocean_health.care.management.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.UserAccount;
import com.blueocean_health.care.management.domain.base.UserToken;
import com.blueocean_health.care.management.domain.vo.UserAccountVo;
import com.blueocean_health.care.management.domain.vo.UserVo;
import com.blueocean_health.care.management.mapper.UserAccountMapper;
import com.blueocean_health.care.management.mapper.UserTokenMapper;
import com.blueocean_health.care.management.service.LoginService;

@Service
public class LoginServiceImpl implements LoginService {
	@Autowired
	private UserTokenMapper userTokenMapper;
	@Autowired
	private UserAccountMapper userDao;

	@Value("${service.address}")
	private String SERVICEADDRESS;

	/**
	 * 通过token 查询用户信息
	 */
	@Override
	public UserVo getUserInfoByToken(String token) {
		UserToken userToken = userTokenMapper.getTokenByToken(token);
		if (userToken == null) {
			return null;
		}
		String userId = userToken.getUserId();
		UserAccount user = new UserAccount();
		user.setUserId(Integer.parseInt(userId));
		// 查询用户详情
		UserAccountVo userAccountVo = userDao.query(userId);

		UserVo userVo = new UserVo();
		userVo.setToken(token);
		userVo.setUserId(userAccountVo.getUserId());
		userVo.setUserName(userAccountVo.getUserName());
		userVo.setUserRealName(userAccountVo.getUserRealName());
		if (!StringUtils.isEmpty(userAccountVo.getEnableStatus())) {
			userVo.setEnableStatus(Integer.parseInt(userAccountVo.getEnableStatus()));
		}
		if (!StringUtils.isEmpty(userAccountVo.getUserSource())) {
			userVo.setUserSource(Integer.parseInt(userAccountVo.getUserSource()));
		}
		// 用户真实选择的组织
		userVo.setTeamId(userToken.getTeamId());
		return userVo;
	}
}
